﻿namespace MtmDeepCopy
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Windows.Data;
    using System.Windows.Media.Imaging;

    public enum ActionExecStatus { Queued, Runing, Done, Failed }

    public class ProgressViewModel : INotifyPropertyChanged
    {
        public Dictionary<string, object> SharedVariables;

        private List<ActionExec> actions;

        public ProgressViewModel()
        {
            actions = new List<ActionExec>();
        }

        public List<ActionExec> Actions
        {
            get 
            { 
                return actions; 
            }
            
            set 
            {
                actions = value;
                NotifyPropertyChanged("Actions");
            }
        }
        
        public void SetActions(List<IAction> lst)
        {
            foreach (IAction a in lst)
            {
                Actions.Add(new ActionExec() { theAction = a, Status = ActionExecStatus.Queued });
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }

    public class ActionExec : INotifyPropertyChanged
    {
        public IAction theAction;

        private ActionExecStatus _status;
        private string _message;

        public string Title 
        { 
            get 
            { 
                return theAction.Title; 
            } 
        }

        public ActionExecStatus Status 
        {
            get { return _status; }
            set { _status = value; NotifyPropertyChanged("Status"); }
        }

        public string Message
        {
            get { return _message; }
            set { _message = value; NotifyPropertyChanged("Message"); }
        }

        public string Log { get; set; }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }

    public class EnumToPicConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch ((ActionExecStatus)value)
            {
                case ActionExecStatus.Queued:
                    return null;
                case ActionExecStatus.Runing:
                    return new BitmapImage(new Uri("pack://application:,,,/TestPlanBuilder;component/Resources/Running.png"));
                case ActionExecStatus.Done:
                    return new BitmapImage(new Uri("pack://application:,,,/TestPlanBuilder;component/Resources/Done.png"));
                case ActionExecStatus.Failed:
                    return new BitmapImage(new Uri("pack://application:,,,/TestPlanBuilder;component/Resources/Failed.png"));
                default:
                    break;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //You could convert back here
            return null;
        }
    }
}
